/** 
 *
 * Lisense:
 * Copyright (c) 2015.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @fileDesc:TODO
 * @author:dairymix
 * @version:1.0.0
 */
package cn.wanghaomiao.utils;

import java.io.IOException;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.wanghaomiao.seimi.struct.Response;
import cn.wanghaomiao.xpath.exception.NoSuchAxisException;
import cn.wanghaomiao.xpath.exception.NoSuchFunctionException;
import cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException;
import cn.wanghaomiao.xpath.model.JXDocument;

/** 
 * @ClassName:SeimiUtils
 * @Description:提取用于操作的方法到utils中
 * @author:dairymix
 * @version:1.0.0
 */
public class SeimiUtils {

    protected static Logger logger = LoggerFactory.getLogger(SeimiUtils.class);

    private static void xPathSel(JXDocument jxDocument, String xPath)
        throws IOException, NoSuchAxisException, NoSuchFunctionException,XpathSyntaxErrorException {
        List<Object> rs = jxDocument.sel(xPath);
        for (Object o : rs) {
            if (o instanceof Element) {
                Element e = ((Element) o);
                System.out.println("node_index:"+e.siblingIndex());
                System.out.println("node_name:"+e.nodeName());
                // logger.info("node_name:", e.nodeName());
                // logger.info("node_index:", e.siblingIndex());
                // logger.info("node_attr:", e.attributes());
                // logger.info("node_tag:", e.tagName());
                // logger.info("node_data:", e.data());
                // logger.info("node_child:", e.children().toString());
                // logger.info("node_parent:", e.parent().toString());
                // logger.info("node_next_index:",
                // e.nextSibling().siblingIndex());
            }
            System.out.println("node_data:"+o.toString());
            // logger.info(((Element) o).nodeName(), ":{} {}", rs);
        }
    }

    public static void xpathTest(Response response,String xPath) 
            throws IOException, NoSuchAxisException, NoSuchFunctionException, XpathSyntaxErrorException {
        xPathSel(response.document(), xPath);
    }

    public static void xpathTest(String curl,String userAgent,String xPath) 
        throws IOException, NoSuchAxisException, NoSuchFunctionException, XpathSyntaxErrorException {
        Document doc = Jsoup.connect(curl).userAgent(userAgent).get();
        JXDocument jxDocument = new JXDocument(doc);
        xPathSel(jxDocument, xPath);
    }

    public static void xpathTest(String curl,String xPath) 
            throws IOException, NoSuchAxisException, NoSuchFunctionException, XpathSyntaxErrorException {
        xpathTest(curl, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0", xPath);
    }

}
